import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description:异常学习
 * User:22954
 * Date:2024-01-16
 * Time:16:09
 */
public class Test1 {
    public static int fuc4(){
        try (Scanner scanner = new Scanner(System.in)) {
            try {
                int a = scanner.nextInt();
                return a;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("finally part is displayed");
                return 100;//finally一定会在最后执行
            }
        }
    }
    public static void main(String[] args) {
        System.out.println(fuc4());
    }
    public static void main3(String[] args) {
/*        Scanner scanner = new Scanner(System.in);
        try {
            int a = scanner.nextInt();
        } catch (InputMismatchException a) {
            System.out.println("hahha");
            a.printStackTrace();
        } finally {
            System.out.println("display finally part");
            scanner.close();
        } */
        /**
         * try加参数，会主动帮忙关闭流
         */
        try (Scanner scanner = new Scanner(System.in)) {
            int a = scanner.nextInt();
        } catch (InputMismatchException a) {
            System.out.println("hahha");
            a.printStackTrace();
        } finally {
            System.out.println("display finally part");
        }
    }

    public static void fuc3() throws Exception {
        throw new Exception();//默认为受查异常，必须处理
    }

    public static void fuc2() {
        throw new RuntimeException();//Runtime类的受查异常，可以交给JVM处理
    }

    public static void fuc1() {
        try {
            throw new NullPointerException();//受查异常，必须处理
        } catch (NullPointerException a) {
            System.out.println("hahaha");
        }
    }

    public static void main2(String[] args) {
        fuc1();
        try {
//            fuc2();
            fuc3();
        } catch (RuntimeException b) {
            System.out.println("fafafa");
        } catch (Exception c) {
            System.out.println("kaikaikai");
        }
    }

    public static void main1(String[] args) {
        try {
            System.out.println("fafafafa");
            int[] array = null;
            System.out.println(array[0]);
            System.out.println(10 / 0);
        } catch (ArithmeticException a) {
            a.printStackTrace();
            System.out.println("算术异常");
        }
        System.out.println("hahahaha");
    }
}
